﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Snowing.Text
{
    public static class Base34Extension
    {
        static readonly string Elements34 = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ";


        public static string ToBase34(this ulong value)
        {
            string result = string.Empty;
            ulong mod = value % 34;
            result += Elements34.Substring((int)mod, 1);

            while (value > 34)
            {
                value = value / 34;
                mod = value % 34;
                result = result.Insert(0, Elements34.Substring((int)mod, 1));
            }
            return result;
        }

        public static ulong ToUlong(this string value)
        {
            ulong result = 0;
            int length = value.Length;
            for (int i = 0; i < length; i++)
            {
                ulong x = (ulong)Elements34.IndexOf(value[i]);
                result += (ulong)Math.Pow(34, length - 1 - i);
            }
            return result;
        }
    }
}
